package com.company;

/**
 * Created by yijy on 2017/5/4.
 */
public class Joining {

    public static void main(String[] args) {
        Sleeper sleeper = new Sleeper("Sleeper",1500);
        Sleeper grumpy = new Sleeper("grumpy",1500);
        Joiner dopey = new Joiner("dopey",sleeper);
        Joiner doc = new Joiner("doc",grumpy);
        grumpy.interrupt();
    }
}

class Sleeper extends Thread{
    private int duration;

    public Sleeper(String name , int sleepTime){
        super(name);
        duration = sleepTime;
        start();
    }

    public void run(){
        try {
            sleep(duration);
        } catch (InterruptedException e) {
            System.out.println(getName()+" was interrupted. "+" isinterrupted(): "+ interrupted() );
        }
        System.out.println(getName()+" has awakened");
    }
}

class Joiner extends Thread{

    private Sleeper sleeper;

    public Joiner(String name , Sleeper sleeper){
        super(name);
        this.sleeper = sleeper;
        start();
    }

    @Override
    public void run() {
        try {
            sleeper.join();
        } catch (InterruptedException e) {
            System.out.println("InterruptedException");
        }
        System.out.println(getName()+" join completed");
    }
}

